<!-- src/views/digital/csc/index.vue -->
<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="软著名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入软著名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="著作权编号" prop="no">
        <el-input
          v-model="queryParams.no"
          placeholder="请输入著作权编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="所属单位" prop="affiliation">
        <el-select
          v-model="queryParams.affiliation"
          placeholder="请选择所属单位"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.AFFILIATION)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="登记号" prop="registrationNum">
        <el-input
          v-model="queryParams.registrationNum"
          placeholder="请输入登记号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="出版日期" prop="publicationTime">
        <el-date-picker
          v-model="queryParams.publicationTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="软著类型" prop="cscType">
        <el-select
          v-model="queryParams.cscType"
          placeholder="请选择软著类型"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.CSC_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="作者" prop="author">
        <el-input
          v-model="queryParams.author"
          placeholder="请输入作者"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="佐证地址" prop="evidenceUrl">
        <el-input
          v-model="queryParams.evidenceUrl"
          placeholder="请输入佐证地址"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['digital:csc:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['digital:csc:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="软著名称" align="center" prop="name" />
      <el-table-column label="著作权编号" align="center" prop="no" />
      <el-table-column label="所属单位" align="center" prop="affiliation">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.AFFILIATION" :value="scope.row.affiliation" />
        </template>
      </el-table-column>
      <el-table-column label="登记号" align="center" prop="registrationNum" />
      <el-table-column 
        label="出版日期"
        align="center"
        prop="publicationTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="软著类型" align="center" prop="cscType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.CSC_TYPE" :value="scope.row.cscType" />
        </template>
      </el-table-column>
      <el-table-column label="作者" align="center" prop="author" width="200">
        <template #default="scope">
          <el-tooltip 
            placement="top"
            v-if="scope.row.author && scope.row.rate"
          >
            <template #content>
              <div v-for="(author, index) in scope.row.author.split(',')" :key="index">
                {{ author }}: {{ scope.row.rate.split(',')[index] }}%
              </div>
            </template>
            <span>{{ scope.row.author }}</span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column 
        label="佐证" 
        align="center" 
        prop="evidenceUrl" 
        min-width="150"
      >
        <template #default="scope">
          <div 
            v-if="scope.row.evidenceUrl && Array.isArray(scope.row.evidenceUrl)"
            class="flex flex-wrap justify-center gap-1"
          >
            <el-link
              v-for="(url, index) in scope.row.evidenceUrl"
              :key="index"
              type="primary"
              :href="url"
              target="_blank"
              :underline="false"
              class="inline-block"
            >
              佐证{{ index + 1 }}
            </el-link>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['digital:csc:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['digital:csc:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗 -->
  <CscForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { CscApi, CscVO } from '@/api/digital/csc'
import CscForm from './CscForm.vue'

/** 软著列表页 */
defineOptions({ name: 'Csc' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表加载状态
const list = ref<CscVO[]>([]) // 列表数据
const total = ref(0) // 总记录数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined,
  no: undefined,
  affiliation: undefined,
  registrationNum: undefined,
  publicationTime: [],
  cscType: undefined,
  author: undefined,
  evidenceUrl: undefined
})
const queryFormRef = ref() // 搜索表单引用
const exportLoading = ref(false) // 导出加载状态

/** 获取列表数据 */
const getList = async () => {
  loading.value = true
  try {
    const data = await CscApi.getCscPage(queryParams)
    
    // 处理证据URL
    data.list.forEach(item => {
      if (typeof item.evidenceUrl === 'string') {
        item.evidenceUrl = item.evidenceUrl
          .replace(/^\[|\]$/g, '')
          .split(',')
          .map(url => url.trim())
          .filter(url => url)
      }
      if (!Array.isArray(item.evidenceUrl)) {
        item.evidenceUrl = []
      }
    })
    
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置搜索 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 打开表单弹窗 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除操作 */
const handleDelete = async (id: number) => {
  try {
    await message.delConfirm()
    await CscApi.deleteCsc(id)
    message.success(t('common.delSuccess'))
    getList()
  } catch {}
}

/** 导出操作 */
const handleExport = async () => {
  try {
    await message.exportConfirm()
    exportLoading.value = true
    const data = await CscApi.exportCsc(queryParams)
    download.excel(data, '软著数据.xls')
  } finally {
    exportLoading.value = false
  }
}

// 初始化
onMounted(() => {
  getList()
})
</script>